/**
 *	Main entry into the app
**/

"use strict"

require("Game/Game.GUI.js")
require("MouseEvents.js")
require("Game/GameLogic.validator.js")
require("Game/GameLogic.winChecker.js")
require("API.js")

var Game = {
	
	init: function() {
		var c = this.c = document.getElementById("canvas")
		var ctx = this.ctx = c.getContext("2d")
		
		c.width = 300
		c.height = 300
		
		ctx.fillStyle = "black"
		ctx.fillRect(0, 0, c.offsetWidth, c.offsetHeight)
		
		//game info prototypes
		this.game = { }
		this.localPlayer = { }
		
		
		MouseEvents.createMouseEvents(c, this.game, this.localPlayer)		//create mouse events on the canvas
		MouseEvents.createTimerEvents(this.game, this.localPlayer)			//create timer events for querying the API

		Game.GUI.init(this.game)											//create GUI
	},
	
	//retrieves game info from the server and sets up a new game
	setUpGame: function(newGameInfo, playerName) {
		//create game template
		var gameTemplate = {
			gameId: null,								//this is set by server response above
			board: { w: 3, h: 3 },
			state: [
				[0, 0, 0],
				[0, 0, 0],
				[0, 0, 0]
			],
			players: null,
			turn: 1,
			turnCount: 0,
			winner: 0
		}
		
		//since we have passes the game object around a lot we can't just delete it, so lets zero it
		for (var i in this.game) {
			delete this.game[i]
		}
		
		//now repopulate it using the gameTemplate above!
		for (var i in gameTemplate) {
			this.game[i] = gameTemplate[i]
		}
		
		//now apply values which came from the server
		for (var i in newGameInfo) {
			this.game[i] = newGameInfo[i]
		}
		
		//zero localPlayer
		for (var i in this.localPlayer) {
			delete this.localPlayer[i]
		}
		
		//set local player info
		this.localPlayer.name = playerName
		this.localPlayer.num = ( this.game.players[0] == playerName ? 1 : 2 )
		console.log(playerName)
		console.log(this.game.players)
		//console.log(JSON.stringify(this.localPlayer))
		
		//temp
		Game.GUI.setPlayers(this.game)
	},
	
	//draw screen
	draw: function() {		
		var ctx = this.ctx, c = this.c
		
		//cls
		ctx.fillStyle = "black"
		ctx.fillRect(0, 0, c.offsetWidth, c.offsetHeight)
		
		//if game board info does not exists yet, give up on draw
		if (!this.game.board)  return
		
		
		//guide lines
		var w = this.game.board.w,
			h = this.game.board.h,
			lw = Math.round( c.offsetWidth / w ),
			lh = Math.round( c.offsetHeight / h )
		
		ctx.fillStyle = "white"
		
		for (var i=1; i<h; i++) {
			ctx.fillRect(0, lh*i, c.offsetWidth, 1)
		}
		for (var i=1; i<w; i++) {
			ctx.fillRect(lw*i, 0, 1, c.offsetHeight)
		}
		
		//markers
		var s = this.game.state
		for (var i=0; i<h; i++) {
			for (var j=0; j<w; j++) {
				if ( s[i][j] != 0 ) {
					this._drawMarker( j, i, lw, lh, s[i][j] )
				}
			}
		}
		
		//GUI
		Game.GUI.draw()
	},
	
	//draws a single marker
	_drawMarker: function(x, y, mwidth, mheight, marker) {
		var ctx = this.ctx,
			offsetX = mwidth * x + Math.floor(mwidth/2),
			offsetY = mwidth * y + Math.floor(mheight/2)
		
		ctx.strokeStyle = "white"
		ctx.beginPath()
			
		if (marker === 1) {
			ctx.arc(offsetX, offsetY, Math.round(mwidth*0.4), 0, Math.PI*2)
		} else {
			var sizetmp = 0.4, ytmp = Math.round(mheight*sizetmp), xtmp = Math.round(mwidth*sizetmp)
			ctx.moveTo(offsetX - xtmp, offsetY - ytmp)
			ctx.lineTo(offsetX + xtmp, offsetY + ytmp)
			ctx.moveTo(offsetX - xtmp, offsetY + ytmp)
			ctx.lineTo(offsetX + xtmp, offsetY - ytmp)
		}
		
		ctx.closePath()
		ctx.stroke()
	},
		
	makeMove: function(x, y) {
		//console.log(x + ", " + y)
		var move = { x: x, y: y }
		
		try {
			GameLogic.validator.validate( this.game, move )  			//check if move is valid
		} catch(err) {
			Game.GUI.userLog(err)											//report to user if invalid
			return
		}
		
		this.game.state[y][x] = this.game.turn  						//make move
		this.game.turn = ( this.game.turn == 1 ? 2 : 1 )				//update current turn
		this.game.turnCount++											//increment number of turns taken this game
		this.game.winner = GameLogic.winChecker.check( this.game )		//check if game has been won
		
		this.draw()														//redraw board
		
		API.sendMove({
			x: x,
			y: y,
			gameId: this.game.gameId,
			turnCount: this.game.turnCount
		})
	}
		
}